package com.njzxw.yoga.yogaApi.server;

import com.njzxw.fs.commonutil.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.njzxw.yoga.yogaApi.repository.TeacherAttrRepository;
import com.njzxw.yoga.yogaApi.bean.TeacherAttrBean;
import com.njzxw.fs.commonutil.web.BaseServer;
import com.njzxw.fs.commonutil.web.ReturnMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TeacherAttrServer extends BaseServer{

    @Autowired
    private TeacherAttrRepository teacherAttrRepository;

    /**
    * 分页获取数据信息
    *
    * @param request
    * @param response
    * @return
    */
    public ReturnMap queryPage(HttpServletRequest request, HttpServletResponse response,TeacherAttrBean teacherAttrBean) {
        if(!StringUtils.isBlank(teacherAttrBean.getOper_date())){
            String[] operDate = teacherAttrBean.getOper_date().split("至");
            if(operDate.length > 1){
                teacherAttrBean.setMax_oper_date(operDate[1].trim());
                teacherAttrBean.setMin_oper_date(operDate[0].trim());
            }
        }
        returnMap.setPageData(teacherAttrRepository.queryPage(teacherAttrBean));
        return returnMap.getSucceeReturnMap();
    }

    /**
     * 获取但一条数据信息
     *
     * @param request
     * @param response
     * @return
     */
    public ReturnMap queryId(HttpServletRequest request, HttpServletResponse response,TeacherAttrBean teacherAttrBean) {
        returnMap.setDataMaps(teacherAttrRepository.queryId(teacherAttrBean));
        return returnMap.getSucceeReturnMap();
    }

    /**
     * 修改某一条数据信息
     *
     * @param request
     * @param response
     * @return
     */
    @Transactional
    public ReturnMap update(HttpServletRequest request, HttpServletResponse response,TeacherAttrBean teacherAttrBean) {
        Integer exe = teacherAttrRepository.update(teacherAttrBean);
        if(exe == null || exe <= 0){
            return returnMap.getErrorReturnMap("修改失败，请稍后再试");
        }
        return returnMap.getSucceeReturnMap();
    }

    /**
     * 删除数据信息
     *
     * @param request
     * @param response
     * @return
     */
    @Transactional
    public ReturnMap delete(HttpServletRequest request, HttpServletResponse response,TeacherAttrBean teacherAttrBean) {
        Integer exe = teacherAttrRepository.delete(teacherAttrBean);
        if(exe == null || exe <= 0){
            return returnMap.getErrorReturnMap("删除失败，请稍后再试");
        }
        return returnMap.getSucceeReturnMap();
    }

    /**
     * 插入数据信息
     *
     * @param request
     * @param response
     * @return
     */
    @Transactional
    public ReturnMap insert(HttpServletRequest request, HttpServletResponse response,TeacherAttrBean teacherAttrBean) {
        Integer exe = teacherAttrRepository.insert(teacherAttrBean);
        if(exe == null || exe <= 0){
            return returnMap.getErrorReturnMap("新增失败，请稍后再试");
        }
        returnMap.setDataMaps(teacherAttrBean.getId());
        return returnMap.getSucceeReturnMap();
    }

    /**
     * 保存操作，根据ID是否存在自动跳转新增还是修改
     * @param request
     * @param response
     * @param teacherAttrBean
     * @return
     */
    @Transactional
    public ReturnMap save(HttpServletRequest request, HttpServletResponse response,TeacherAttrBean teacherAttrBean){
        logger.debug("teacherBean:"+teacherAttrBean);
        if(!StringUtils.isBlank(teacherAttrBean.getId())){
            return this.update(request,response,teacherAttrBean);
        }else{
            return this.insert(request,response,teacherAttrBean);
        }
    }

    public ReturnMap importTeacherAttr(HttpServletRequest request, HttpServletResponse response,TeacherAttrBean teacherAttrBean) {

        String fileDir = "/user/import/";//文件下载到那个目录，用于上传远程文件服务器使用
        String importTable = "t_manager_user";//主要插入表的名称

        /**
         * 验证基本读取数据，传入正则校验，具体如下注释掉的内容
         */
        List<Map<String,String>> verifyList = new ArrayList<>();

//        Map<String,String> map = new HashMap<>();
//        map.put("col","col0");
//        map.put("regex", Validator.REGEX_NOT_EMPTY);
//        map.put("msg","用户名不能为空");
//        verifyList.add(map);
//
//        Map<String,String> map1 = new HashMap<>();
//        map1.put("col","col1");
//        map1.put("regex", Validator.REGEX_EMAIL);
//        map1.put("msg","email号格式不符合");
//        verifyList.add(map1);
//
//        Map<String,String> map2 = new HashMap<>();
//        map2.put("col","col2");
//        map2.put("regex", Validator.REGEX_MOBILE);
//        map2.put("msg","手机号格式不符合");
//        verifyList.add(map2);
//
//        Map<String,String> map3 = new HashMap<>();
//        map3.put("col","col3");
//        map3.put("regex", Validator.YEAR_MONTH_DAY);
//        map3.put("msg","生日格式不符合要求，只能是如2018-02-03");
//        verifyList.add(map3);

        /**
         * 校验sql语句，支持插入数据前进行sql语句校验，如校验当前excel中重复内容，或者与主表比较重复内容
         *
         * #tmptable 为当前的临时表数据，也就是excel数据
         *
         */
        List<String> verifySql = new ArrayList<>();
//        verifySql.add("update #tmptable c set c.errorMsg = CONCAT(ifnull(errorMsg,''),',','存在相同email号') \n" +
//        "where exists(select 1 from (select a.id from #tmptable a,t_manager_user b where a.col1 = b" +
//        ".`email` ) aa where aa.id = c.id) ");
//        verifySql.add("update #tmptable c set c.errorMsg = CONCAT(ifnull(errorMsg,''),',','存在相同手机号') \n" +
//        "where exists(select 1 from (select a.id from #tmptable a,t_manager_user b where a.col2 = b" +
//        ".`mobile` ) aa where aa.id = c.id) ");
//        verifySql.add("update #tmptable a inner join #tmptable b on b.col2 = a.col2 and a.id <> b.id \n" +
//        "set a.errorMsg = CONCAT(ifnull(a.errorMsg,''),',','当前列表数据中存在相同手机号') where 1=1 ");
//        verifySql.add("update #tmptable a inner join #tmptable b on b.col1 = a.col1 and a.id <> b.id \n" +
//        " set a.errorMsg = CONCAT(ifnull(a.errorMsg,''),',','当前列表数据中存在相同email号') where 1=1 ");

        /**
         * 插入sql语句，需要插入的表数据，写法如下
         */
        List<String> insertSql = new ArrayList<>();

//        insertSql.add("insert into t_manager_user(id,name,email,mobile,bir,addr)select a.id,a.col0,a.col1,a.col2,a.col3,a.col4 from #tmptable a");

        //下面ImportTableExecuteInterface中如果没有额外需要处理内容，直接传null
        return this.importFile(fileDir, importTable, verifySql, insertSql, verifyList, teacherAttrBean, new ExcelUtils
            .ImportTableExecuteInterface() {
            /**
             * 在插入数据之前进行操作
             *
             * @param tableName
             * @param list
             */
            @Override
            public void previous(String tableName, List<Map<String, Object>> list) {
                //在此处写你的代码
            }

            /**
             * 在主要表数据插入完成后执行操作
             */
            @Override
            public void upper(String tableName, List<Map<String, Object>> list) {
                //在此处写你的代码
            }
        });
    }

}